package com.design.pattern.memo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 棋盘
 */
public class CheckerBoard {
    private final Integer MAX_TIME=30000;//最长思考时间30秒
    private List<Piece> list=new ArrayList<>();

    public void setList(List<Piece> list) {
        this.list = list;
    }

    public List<Piece> getList() {
        return list;
    }

    private Date lastTime=new Date();
    /**
     *  落子
     * @param piece 棋子数据
     */
    public void down(Piece piece){

        Date time=new Date();
        piece.setThinkingTime(time.getTime()-lastTime.getTime());
        lastTime=time; //初始化上一次落子时间
        list.add(piece);
        //备份当前棋盘记录

    }

    /**
     * 获取当前棋盘备份
     * @return
     */
    public CheckerBoardMemo createMemo(){
        return new CheckerBoardMemo().setPieces(list);
    }
    public void resetCheckerBoardMemo(CheckerBoardMemoIF checkerBoardMemo){
        //撤销棋盘操作
        this.setList(((CheckerBoardMemo)checkerBoardMemo).getPieces());
    }
    public void show(){
        System.out.println("当前棋子数量:"+list.size());
        for (Piece piece:list){
            System.out.println(
                    (piece.getColor()==PieceColor.black?"黑":"白")+
                            "棋:位置{X="+piece.getCoordinateX()+"," +
                            "Y="+piece.getCoordinateY()+
                            "},使用时间:"+piece.getThinkingTime());
        }
    }

    /**
     * 通过内部类的,限制外部访问
     */
    private class CheckerBoardMemo implements CheckerBoardMemoIF {
       private List<Piece> pieces;

        public List<Piece> getPieces() {
            return pieces;
        }

        public CheckerBoardMemo setPieces(List<Piece> pieces) {
            this.pieces=new ArrayList<>();
            this.pieces.addAll(pieces);
            return this;
        }
    }
}
